package com.hltx.controller.app.pay;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hltx.constants.PayConstant;
import com.hltx.controller.base.BaseController;
import com.hltx.pojo.pay.PayInfo;
import com.hltx.pojo.user.UserLogin;
import com.hltx.service.brokerage.impl.ConsumeBrokerageV2Service;
import com.hltx.service.pay.AlipayPayManager;
import com.hltx.service.pay.OfflinePayManager;
import com.hltx.util.CustomUtil;
import com.hltx.util.DataUtil;
import com.hltx.util.ResultInfo;

@Controller
@RequestMapping(value = "/app/pay/")
public class AppPayController extends BaseController {
	
	@Resource(name="sqlSessionFactory")
	private SqlSessionFactory factory;
	
	@Autowired
	private OfflinePayManager offlinePayService;
	
	@Autowired
	private ConsumeBrokerageV2Service brokerageV2Service;
	
	@Autowired
	private AlipayPayManager alipayPayService;
	
	/**
	 * App通过商家扫码余额支付
	 * @return
	 */
	@RequestMapping(value = "balancePay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appBalancePay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "余额支付");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String loginIdIdr = request.getParameter("loginId");
		String payPwd = request.getParameter("payPwd");
		String tag = request.getParameter("tag");
		String userLevel = request.getParameter("userLevel");
		int loginId = Integer.parseInt(loginIdIdr);
		UserLogin userLogin = getPayPwdByLoginId(loginId);
		if (userLogin == null) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "payPwdNull", "支付密码不存在"));
			return resMap;
		}
		
		if(StringUtils.isEmpty(payPwd) || !StringUtils.equals(payPwd, userLogin.getPayPwd())){
			resMap.put("result", this.initResult(ResultInfo.ERROR, "payPwdError", "支付密码不正确"));
			return resMap;
		}

	    String idStr = request.getParameter("id");
	    String payCode = request.getParameter("payCode");
	    int len = StringUtils.length(payCode);
	    Integer id = null;
	    if (idStr != null) {
	    	id = Integer.parseInt(idStr);
	    	try {
	    		String codePre = payCode.substring(0, 4);
	    		String codeSuf = payCode.substring(len - 7);
	    		long payId = Long.parseLong(payCode.substring(4, len - 7));
	    		Map<String, Object> payParams = new HashMap<String, Object>();
	    		payParams.put("id", payId);
	    		payParams.put("codePre", codePre);
	    		payParams.put("codeSuf", codeSuf);
	    		PayInfo pi = offlinePayService.getPayInfoById(payParams);
	    	
	    		int totalPointsNum = pi.getTotalPointsNum();
	    		
	    		if(totalPointsNum > 0){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "网络异常"));
	    			return resMap;
	    		}
	    		
	    		BigDecimal totalPrice = pi.getTotalPrice();
	    		
	    		if(totalPrice == null || totalPrice.compareTo(BigDecimal.ZERO) <=0){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "支付金额有误"));
	    			return resMap;
	    		}
	    		
	    		if(StringUtils.equals(pi.getState(),PayConstant.PAY_SUCCESS) || StringUtils.equals(pi.getState(),PayConstant.PAY_CANCEL)){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "payCancel", "该单已支付或已取消"));
	    			return resMap;
	    		}
	    		
	    		Map<String, Object> params = new HashMap<String, Object>();
	    		
	    		pi.setBalancePrice(totalPrice);
	    		pi.setCodePre(codePre);
	    		pi.setCodeSuf(codeSuf);
	    		pi.setUserId(id);
	    		pi.setTag(tag);
	    		pi.setUserLevel(userLevel);
	    		params.put("pi", pi);
	    		ResultInfo resInfo = offlinePayService.offlineCbalancePayOrder(params);
	    		resMap.put("result", resInfo);
	    		if (StringUtils.equals(resInfo.getState(), ResultInfo.SUCCESS)) {
	    			brokerageV2Service.offlineBounsShunt(pi);
	    		}
	    	} catch (Exception e) {
	    		// TODO Auto-generated catch block
	    		e.printStackTrace();
	    		resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", null));
	    	}
	}


		return resMap;
	}
	
	/**
	 * App通过商家扫码支付宝支付
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "alipayPay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appAliPay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "APP支付宝支付");
		Map<String, Object> resMap = new HashMap<String, Object>();
	    String idStr = request.getParameter("id");
	    String payCode = request.getParameter("payCode");
	    String userLevel = request.getParameter("userLevel");
	    String tag = request.getParameter("tag");
	    int len = StringUtils.length(payCode);
	    Integer id = null;
	    if (idStr != null) {
	    	id = Integer.parseInt(idStr);
	    	try {
	    		String codePre = payCode.substring(0, 4);
	    		String codeSuf = payCode.substring(len - 7);
	    		long payId = Long.parseLong(payCode.substring(4, len - 7));
	    		Map<String, Object> payParams = new HashMap<String, Object>();
	    		payParams.put("id", payId);
	    		payParams.put("codePre", codePre);
	    		payParams.put("codeSuf", codeSuf);
	    		PayInfo pi = offlinePayService.getPayInfoById(payParams);
	    		
	    		if("00".equals(pi.getState())){
	    			
	    		}
	    		BigDecimal totalPrice = pi.getTotalPrice();
	    		
	    		if(totalPrice == null || totalPrice.compareTo(BigDecimal.ZERO) <=0){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "支付金额有误"));
	    			return resMap;
	    		}
	    		
	    		if(StringUtils.equals(pi.getState(),PayConstant.PAY_SUCCESS) || StringUtils.equals(pi.getState(),PayConstant.PAY_CANCEL)){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "payCancel", "该单已支付或已取消"));
	    			return resMap;
	    		}
	    		
	    		Map<String, Object> params = new HashMap<String, Object>();
	    		pi.setAddPrice(totalPrice.subtract(DataUtil.getBigDecimal(pi.getTotalPointsNum())));
	    		pi.setCodePre(codePre);
	    		pi.setCodeSuf(codeSuf);
	    		pi.setUserId(id);
	    		pi.setTag(tag);
	    		pi.setUserLevel(userLevel);
	    		params.put("pi", pi);
	    		Map<String, Object> saveRes = alipayPayService.saveOfflineAlipayPayOrder(params);
	    		ResultInfo ri = (ResultInfo)saveRes.get("result");
	    		if(!ResultInfo.SUCCESS.equals(ri.getState())){
	    			return saveRes;
	    		}
	    		
	    		Map<String, String> resParams = (Map<String, String>) saveRes.get("resParams");
	    		Map<String, String> returnParams = (Map<String, String>) saveRes.get("returnParams");
				String contextPath = request.getContextPath();
				String url = alipayPayService.alipayApp(returnParams, contextPath);
				
				if (StringUtils.isNotEmpty(url)) {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, "OK", null));
					resMap.put("url", url);
					resMap.put("resParams", resParams);
				}else{
					resMap.put("result", this.initResult(ResultInfo.ERROR, "fail","系统繁忙", null));
				}
	    	} catch (Exception e) {
	    		// TODO Auto-generated catch block
	    		e.printStackTrace();
	    		resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", null));
	    	}
	}


		return resMap;
	}
	
	/**
	 * App通过商家扫码微信支付
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "weChatPay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appWeChatPay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "APP微信支付");
		Map<String, Object> resMap = new HashMap<String, Object>();
	    String idStr = request.getParameter("id");
	    String payCode = request.getParameter("payCode");
	    String userLevel = request.getParameter("userLevel");
	    String tag = request.getParameter("tag");
	    int len = StringUtils.length(payCode);
	    Integer id = null;
	    if (idStr != null) {
	    	id = Integer.parseInt(idStr);
	    	try {
	    		String codePre = payCode.substring(0, 4);
	    		String codeSuf = payCode.substring(len - 7);
	    		long payId = Long.parseLong(payCode.substring(4, len - 7));
	    		Map<String, Object> payParams = new HashMap<String, Object>();
	    		payParams.put("id", payId);
	    		payParams.put("codePre", codePre);
	    		payParams.put("codeSuf", codeSuf);
	    		PayInfo pi = offlinePayService.getPayInfoById(payParams);
   		
	    		BigDecimal totalPrice = pi.getTotalPrice();
	    		
	    		if(totalPrice == null || totalPrice.compareTo(BigDecimal.ZERO) <=0){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "支付金额有误"));
	    			return resMap;
	    		}
	    		
	    		if(StringUtils.equals(pi.getState(),PayConstant.PAY_SUCCESS) || StringUtils.equals(pi.getState(),PayConstant.PAY_CANCEL)){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "payCancel", "该单已支付或已取消"));
	    			return resMap;
	    		}
	    		
	    		Map<String, Object> params = new HashMap<String, Object>();
	    		pi.setAddPrice(totalPrice.subtract(DataUtil.getBigDecimal(pi.getTotalPointsNum())));
	    		pi.setCodePre(codePre);
	    		pi.setCodeSuf(codeSuf);
	    		pi.setUserId(id);
	    		pi.setUserLevel(userLevel);
	    		pi.setTag(tag);
	    		params.put("pi", pi);
	    		params.put("contextPath", request.getContextPath());
	    		Map<String, Object> saveRes = alipayPayService.saveOfflineWeChatPayOrder(params);
	    		ResultInfo ri = (ResultInfo)saveRes.get("result");
	    		if(!ResultInfo.SUCCESS.equals(ri.getState())){
	    			return saveRes;
	    		}
	    		
	    		Map<String, String> resParams = (Map<String, String>) saveRes.get("resParams");
	    		if(saveRes.containsKey("prePayInfo")){
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, "OK", null));
					resMap.put("prePayInfo", saveRes.get("prePayInfo"));
					resMap.put("resParams", resParams);
					resMap.put("payCode",pi.getCodePre()+pi.getId()+pi.getCodeSuf());
				}else{
					resMap.put("result", this.initResult(ResultInfo.ERROR, "fail","系统繁忙", null));
				}
	    	} catch (Exception e) {
	    		// TODO Auto-generated catch block
	    		e.printStackTrace();
	    		resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", null));
	    	}
	}


		return resMap;
	}
	
	/**
	 * App通过商家扫码积分余额支付
	 * @return
	 */
	@RequestMapping(value = "pointsBalancePay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appPointsBalancePay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "兑换券余额支付");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String loginIdIdr = request.getParameter("loginId");
		String payPwd = request.getParameter("payPwd");
		String userLevel = request.getParameter("userLevel");
		String tag = request.getParameter("tag");
		int loginId = Integer.parseInt(loginIdIdr);
		UserLogin userLogin = getPayPwdByLoginId(loginId);
		
		if (userLogin == null) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "payPwdNull", "支付密码不存在"));
			return resMap;
		}
		
		if (StringUtils.isEmpty(payPwd) || !StringUtils.equals(payPwd, userLogin.getPayPwd())) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "payPwdError", "支付密码不正确"));
			return resMap;
		}
		
		String idStr = request.getParameter("id");
		String payCode = request.getParameter("payCode");
		int len = StringUtils.length(payCode);
		Integer id = null;
		if (idStr != null) {
			id = Integer.parseInt(idStr);
			try {
				String codePre = payCode.substring(0, 4);
				String codeSuf = payCode.substring(len - 7);
				long payId = Long.parseLong(payCode.substring(4, len - 7));
				Map<String, Object> payParams = new HashMap<String, Object>();
				payParams.put("id", payId);
				payParams.put("codePre", codePre);
				payParams.put("codeSuf", codeSuf);
				PayInfo pi = offlinePayService.getPayInfoById(payParams);
				
	    		int totalPointsNum = pi.getTotalPointsNum();
	    		
	    		if(totalPointsNum <= 0){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "支付兑换券有误"));
	    			return resMap;
	    		}
	    		
	    		BigDecimal totalPrice = pi.getTotalPrice();
	    		
	    		if(totalPrice == null || totalPrice.compareTo(BigDecimal.ZERO) <=0){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "网络异常"));
	    			return resMap;
	    		}
				
				
				if(StringUtils.equals(pi.getState(),PayConstant.PAY_SUCCESS) || StringUtils.equals(pi.getState(),PayConstant.PAY_CANCEL)){
	    			resMap.put("result", this.initResult(ResultInfo.ERROR, "payCancel", "该单已支付或已取消"));
	    			return resMap;
	    		}
				
				Map<String, Object> params = new HashMap<String, Object>();
				BigDecimal balancePrice = totalPrice.subtract(DataUtil.getBigDecimal(totalPointsNum));
				pi.setBalancePrice(balancePrice);
				pi.setCodePre(codePre);
				pi.setCodeSuf(codeSuf);
				pi.setUserId(id);
				pi.setUserLevel(userLevel);
				pi.setTag(tag);
				params.put("pi", pi);
				ResultInfo resInfo = offlinePayService.offlinePointsBalancePayOrder(params);
				resMap.put("result", resInfo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", null));
			}
		}
		
		return resMap;
	}
	
	/**
	 * App扫码手动输入余额付款
	 * @return
	 */
	@RequestMapping(value = "manualBalancePay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appManualBalancePay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "手动输入余额支付");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String loginIdIdr = request.getParameter("loginId");
		String payPwd = request.getParameter("payPwd");
		String userLevel = request.getParameter("userLevel");
		String tag = request.getParameter("tag");
		int loginId = Integer.parseInt(loginIdIdr);
		UserLogin userLogin = getPayPwdByLoginId(loginId);
		
		if (userLogin == null) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "payPwdNull", "支付密码不存在"));
			return resMap;
		}

		if (StringUtils.isEmpty(payPwd) || !StringUtils.equals(payPwd, userLogin.getPayPwd())) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "payPwdError", "支付密码不正确"));
			return resMap;
		}

		try {
			String idStr = request.getParameter("id");
			String businessIdStr = request.getParameter("businessId");
			String payBalanceStr = request.getParameter("money");
			int userId = Integer.parseInt(idStr);
			int businessId = Integer.parseInt(businessIdStr);
			BigDecimal payBalance = DataUtil.getBigDecimal(payBalanceStr);
			PayInfo pi = new PayInfo();
			pi.setUserId(userId);
			pi.setBusinessId(businessId);
			pi.setBalancePrice(payBalance);
			pi.setUserLevel(userLevel);
			pi.setTag(tag);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("pi", pi);
			ResultInfo resInfo = offlinePayService.offlineManualBalancePay(params);
			resMap.put("result", resInfo);
			if (StringUtils.equals(resInfo.getState(), ResultInfo.SUCCESS)) {
				brokerageV2Service.offlineBounsShunt(pi);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", null));
		}

		return resMap;
	}
	
	/**
	 * App扫码手动输入线下支付宝付款
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "lineManualBalancePay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appLineManualBalancePay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "手动输入线下支付宝付款");
		Map<String, Object> resMap = new HashMap<String, Object>();

		try {
			String idStr = request.getParameter("id");
			String businessIdStr = request.getParameter("businessId");
			String payBalanceStr = request.getParameter("money");
			String userLevel = request.getParameter("userLevel");
			String tag = request.getParameter("tag");
			int userId = Integer.parseInt(idStr);
			int businessId = Integer.parseInt(businessIdStr);
			BigDecimal payBalance = DataUtil.getBigDecimal(payBalanceStr);
			PayInfo pi = new PayInfo();
			pi.setUserId(userId);
			pi.setBusinessId(businessId);
			pi.setAddPrice(payBalance);
			pi.setUserLevel(userLevel);
			pi.setTag(tag);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("pi", pi);
			Map<String, Object> saveRes = alipayPayService.saveOfflineMualAlipayPayOrder(params);
			
			Map<String, String> resParams = (Map<String, String>) saveRes.get("resParams");
    		Map<String, String> returnParams = (Map<String, String>) saveRes.get("returnParams");
			String contextPath = request.getContextPath();
			String url = alipayPayService.alipayApp(returnParams, contextPath);
			
			if (StringUtils.isNotEmpty(url)) {
				resMap.put("result", this.initResult(ResultInfo.SUCCESS, "OK", null));
				resMap.put("url", url);
				resMap.put("resParams", resParams);
			}else{
				resMap.put("result", this.initResult(ResultInfo.ERROR, "fail","系统繁忙", null));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("result", this.initResult(ResultInfo.ERROR, "系统繁忙", null));
		}

		return resMap;
	}
	
	/**
	 * App扫码手动输入线下微信付款
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "lineManualWeChatPay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appLineManualWeChatPay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "手动输入线下微信付款");
		Map<String, Object> resMap = new HashMap<String, Object>();

		try {
			String idStr = request.getParameter("id");
			String businessIdStr = request.getParameter("businessId");
			String payBalanceStr = request.getParameter("money");
			String userLevel = request.getParameter("userLevel");
			String tag = request.getParameter("tag");
			int userId = Integer.parseInt(idStr);
			int businessId = Integer.parseInt(businessIdStr);
			BigDecimal payBalance = DataUtil.getBigDecimal(payBalanceStr);
			PayInfo pi = new PayInfo();
			pi.setUserId(userId);
			pi.setBusinessId(businessId);
			pi.setAddPrice(payBalance);
			pi.setUserLevel(userLevel);
			pi.setTag(tag);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("pi", pi);
			params.put("contextPath", request.getContextPath());
			Map<String, Object> saveRes = alipayPayService.saveOfflineMualWeChatPay(params);
			if(saveRes.containsKey("prePayInfo")){
				Map<String, String> resParams = (Map<String, String>) saveRes.get("resParams");
				resMap.put("result", this.initResult(ResultInfo.SUCCESS, "OK", null));
				resMap.put("prePayInfo", saveRes.get("prePayInfo"));
				resMap.put("resParams", resParams);
				resMap.put("payCode",pi.getCodePre()+pi.getId()+pi.getCodeSuf());
			}else{
				resMap.put("result", this.initResult(ResultInfo.ERROR, "fail","系统繁忙", null));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", null));
		}

		return resMap;
	}
	
	/**
	 * 新版本APP线下余额支付
	 * @return
	 */
	@RequestMapping(value = "lineBalancePay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appLineBalancePay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "新版本APP线下余额支付");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String loginIdIdr = request.getParameter("loginId");
		String payPwd = request.getParameter("payPwd");
		String userLevel = request.getParameter("userLevel");
		String tag = request.getParameter("tag");
		int loginId = Integer.parseInt(loginIdIdr);
		UserLogin userLogin = getPayPwdByLoginId(loginId);
		
		if (userLogin == null) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "payPwdNull", "支付密码不存在"));
			return resMap;
		}

		if (StringUtils.isEmpty(payPwd) || !StringUtils.equals(payPwd, userLogin.getPayPwd())) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "payPwdError", "支付密码不正确"));
			return resMap;
		}

		try {
			String idStr = request.getParameter("id");
			String businessIdStr = request.getParameter("businessId");
			String payBalanceStr = request.getParameter("money");
			int points = Integer.parseInt(request.getParameter("points"));
			int userId = Integer.parseInt(idStr);
			int businessId = Integer.parseInt(businessIdStr);
			BigDecimal payBalance = DataUtil.getBigDecimal(payBalanceStr);
			PayInfo pi = new PayInfo();
			pi.setUserId(userId);
			pi.setBusinessId(businessId);
			pi.setBalancePrice(payBalance);
			pi.setTotalPointsNum(points);
			pi.setUserLevel(userLevel);
			pi.setTag(tag);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("pi", pi);
			ResultInfo resInfo = offlinePayService.offlineBalancePay(params);
			resMap.put("result", resInfo);
			if (StringUtils.equals(resInfo.getState(), ResultInfo.SUCCESS)) {
				if(pi.getTotalPointsNum() <= 0){
					brokerageV2Service.offlineBounsShunt(pi);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", null));
		}

		return resMap;
	}
	
	/**
	 *新版本APP线下支付宝支付
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "lineAlipayPay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appLineAlipayPay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "新版本APP线下支付宝付款");
		Map<String, Object> resMap = new HashMap<String, Object>();

		try {
			String idStr = request.getParameter("id");
			String businessIdStr = request.getParameter("businessId");
			String payBalanceStr = request.getParameter("money");
			int points = Integer.parseInt(request.getParameter("points"));
			String tag = request.getParameter("tag");
			int userId = Integer.parseInt(idStr);
			int businessId = Integer.parseInt(businessIdStr);
			BigDecimal payBalance = DataUtil.getBigDecimal(payBalanceStr);
			PayInfo pi = new PayInfo();
			pi.setUserId(userId);
			pi.setBusinessId(businessId);
			pi.setAddPrice(payBalance);
			pi.setTotalPointsNum(points);
			pi.setTag(tag);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("pi", pi);
			Map<String, Object> saveRes = alipayPayService.saveOfflineAlipayPay(params);
			ResultInfo ri = (ResultInfo)saveRes.get("result");
    		if(!ResultInfo.SUCCESS.equals(ri.getState())){
    			return saveRes;
    		}
			Map<String, String> resParams = (Map<String, String>) saveRes.get("resParams");
    		Map<String, String> returnParams = (Map<String, String>) saveRes.get("returnParams");
			String contextPath = request.getContextPath();
			String url = alipayPayService.alipayApp(returnParams, contextPath);
			
			if (StringUtils.isNotEmpty(url)) {
				resMap.put("result", this.initResult(ResultInfo.SUCCESS, "OK", null));
				resMap.put("url", url);
				resMap.put("resParams", resParams);
			}else{
				resMap.put("result", this.initResult(ResultInfo.ERROR, "fail","系统繁忙", null));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("result", this.initResult(ResultInfo.ERROR, "系统繁忙", null));
		}

		return resMap;
	}
	
	/**
	 * 新版本APP线下微信支付
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "lineWeChatPay", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appLineWeChatPay (HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "新版本APP线下微信付款");
		Map<String, Object> resMap = new HashMap<String, Object>();

		try {
			String idStr = request.getParameter("id");
			String businessIdStr = request.getParameter("businessId");
			String payBalanceStr = request.getParameter("money");
			int points = Integer.parseInt(request.getParameter("points"));
			String tag = request.getParameter("tag");
			int userId = Integer.parseInt(idStr);
			int businessId = Integer.parseInt(businessIdStr);
			BigDecimal payBalance = DataUtil.getBigDecimal(payBalanceStr);
			PayInfo pi = new PayInfo();
			pi.setUserId(userId);
			pi.setBusinessId(businessId);
			pi.setAddPrice(payBalance);
			pi.setTotalPointsNum(points);
			pi.setTag(tag);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("pi", pi);
			params.put("contextPath", request.getContextPath());
			Map<String, Object> saveRes = alipayPayService.saveOfflineWeChatPay(params);
			if(saveRes.containsKey("prePayInfo")){
				Map<String, String> resParams = (Map<String, String>) saveRes.get("resParams");
				resMap.put("result", this.initResult(ResultInfo.SUCCESS, "OK", null));
				resMap.put("prePayInfo", saveRes.get("prePayInfo"));
				resMap.put("resParams", resParams);
				resMap.put("payCode",pi.getCodePre()+pi.getId()+pi.getCodeSuf());
			}else{
				resMap.put("result", this.initResult(ResultInfo.ERROR, "fail","系统繁忙", null));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", null));
		}

		return resMap;
	}
	
	@RequestMapping(value = "/confirm_alipay_pay", method = { RequestMethod.POST, RequestMethod.GET })
	public void alipayWapPay(PayInfo pi,HttpServletRequest request, HttpSession session, 
			HttpServletResponse response){
		try{
		Map<String, String> params = new HashMap<String, String>();
		
		params.put("outTradeNo", String.valueOf(CustomUtil.buildRandom(6))+String.valueOf(CustomUtil.buildRandom(6)));
		params.put("subject", "苹果5S");
		params.put("totalAmount", "5");
		params.put("body", "手机");
		
		String contextPath = request.getContextPath(); 
		String url = alipayPayService.alipayApp(params, contextPath);
		
		System.out.println(url);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public UserLogin getPayPwdByLoginId(int id){
		SqlSession session = factory.openSession();
		UserLogin userLogin = session.selectOne("com.hltx.mapper.pay.PayMapper.getPayPwdByLoginId", id);
		session.commit();
		session.close();
		return userLogin;
	}

}
